package com.xmy.cultivate.service.impl;

import com.xmy.cultivate.entity.Quarter;
import com.xmy.cultivate.entity.StudentResidueCourse;
import com.xmy.cultivate.entity.TrackDetail;
import com.xmy.cultivate.entity.TrackDetailHistorry;
import com.xmy.cultivate.mapper.StudentResidueCourseMapper;
import com.xmy.cultivate.service.IQuarterService;
import com.xmy.cultivate.service.IStudentResidueCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmy.cultivate.service.ITrackDetailHistorryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 * 已分班学生剩余课次记录【缴费率使用】 服务实现类
 * </p>
 *
 * @author hpiggy
 * @since 2024-06-19
 */
@Service
public class StudentResidueCourseServiceImpl extends ServiceImpl<StudentResidueCourseMapper, StudentResidueCourse> implements IStudentResidueCourseService {

    @Autowired
    StudentResidueCourseMapper residueCourseMapper;

    @Autowired
    IQuarterService iQuarterService;

    @Autowired
    ITrackDetailHistorryService iTrackDetailHistorryService;

    @Async("onlineAsync")
    @Override
    public void addBatch(Integer yearPart,
                            Integer quarterNum,
                            Integer lessonType){

        if (yearPart == null){
            Quarter quarter = iQuarterService.getNowQuarter();
            if (quarter != null){
                yearPart = quarter.getYearPart();
                quarterNum = quarter.getNum();
            }else {
                yearPart = 0;
                quarterNum = 0;
            }
        }


        LocalDate dateNow = LocalDate.now();
        dateNow = dateNow.minusDays(1);//由于定时的是零晨后，以后要减一天

        //先更新战队信息
        List<TrackDetail> trackDetailList = residueCourseMapper.getTrackDetailList(yearPart,quarterNum);
        for (TrackDetail trackDetail:trackDetailList){
            try{
                TrackDetailHistorry trackDetailHistory = residueCourseMapper.getTrackDetailOne(yearPart,quarterNum,trackDetail.getTrackId(),trackDetail.getSourseId());
                if (trackDetailHistory == null){
                    trackDetailHistory = new TrackDetailHistorry();
                    BeanUtils.copyProperties(trackDetail,trackDetailHistory);
                    trackDetailHistory.setStartDate(dateNow);
                    trackDetailHistory.setEndDate(dateNow);
                    iTrackDetailHistorryService.save(trackDetailHistory);
                }else {
                    if (!trackDetailHistory.getSchoolIdStr().equals(trackDetail.getSchoolIdStr()) || !trackDetailHistory.getYearClassIdStr().equals(trackDetail.getYearClassIdStr()) || !trackDetailHistory.getSubjectsIdStr().equals(trackDetail.getSubjectsIdStr()) || !trackDetailHistory.getClassTypeIdStr().equals(trackDetail.getClassTypeIdStr())){
                        if (trackDetailHistory.getEndDate().equals(dateNow)){
                            trackDetailHistory.setEndDate(dateNow.minusDays(1));
                            iTrackDetailHistorryService.updateById(trackDetailHistory);
                        }
                        BeanUtils.copyProperties(trackDetail,trackDetailHistory);
                        trackDetailHistory.setStartDate(dateNow);
                        trackDetailHistory.setEndDate(dateNow);
                        iTrackDetailHistorryService.save(trackDetailHistory);
                    }else {
                        trackDetailHistory.setEndDate(dateNow);
                        iTrackDetailHistorryService.updateById(trackDetailHistory);
                    }
                }
            }catch (Exception e){

            }
        }

        List<StudentResidueCourse> studentResidueCourseList = this.getResidueCourse(yearPart,quarterNum,lessonType);

        StudentResidueCourse studentResidueCourse;
        for (StudentResidueCourse residueCourse:studentResidueCourseList){
            try {
                studentResidueCourse = residueCourseMapper.getOneForDate(residueCourse.getYearPart(),residueCourse.getQuarterNum(),residueCourse.getSubjectsId(),residueCourse.getStudentId(),residueCourse.getLessonType(),dateNow);
                //不存在
                if (studentResidueCourse == null){
                    studentResidueCourse = new StudentResidueCourse();
                    BeanUtils.copyProperties(residueCourse,studentResidueCourse);
                    studentResidueCourse.setStartDate(dateNow);
                    studentResidueCourse.setEndDate(dateNow);
                    this.save(studentResidueCourse);
                }else {
                    if (residueCourse.getResidueCourseCount().equals(studentResidueCourse.getResidueCourseCount()) && residueCourse.getReadingStatus().equals(studentResidueCourse.getReadingStatus())){
                        studentResidueCourse.setEndDate(dateNow);
                        this.updateById(studentResidueCourse);
                    }else {
                        //如果上一条数据的结束时间与当前数据相同，调整为减1天
                        if (studentResidueCourse.getEndDate().isEqual(dateNow)){
                            studentResidueCourse.setEndDate(dateNow.minusDays(1));
                            this.updateById(studentResidueCourse);
                        }
                        studentResidueCourse = new StudentResidueCourse();
                        BeanUtils.copyProperties(residueCourse,studentResidueCourse);
                        studentResidueCourse.setStartDate(dateNow);
                        studentResidueCourse.setEndDate(dateNow);
                        this.save(studentResidueCourse);
                    }
                }
            }catch (Exception e){
            }
        }
    }

    @Override
    public List<StudentResidueCourse> getResidueCourse(Integer yearPart,
                                                       Integer quarterNum,
                                                       Integer lessonType) {
        return residueCourseMapper.getResidueCourse(yearPart,quarterNum,lessonType);

    }
}
